package com.jzo2o.foundations.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {

    @Resource
    private ServeItemMapper serveItemMapper;

    @Resource
    private RegionMapper regionMapper;

    /**
     * 区域服务分页查询
     *
     * @param servePageQueryReqDTO
     * @return
     */
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {
        return PageHelperUtils.selectPage(servePageQueryReqDTO,
                () -> baseMapper.queryServeListByRegionId(servePageQueryReqDTO.getRegionId()));
    }

    /**
     * 添加区域服务项
     *
     * @param serveUpsertReqDTOList
     */
    @Override
    public void saveBatch(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
        for (ServeUpsertReqDTO se : serveUpsertReqDTOList) {
            //根据服务项id判断是否存在和启用
            ServeItem serveItem = serveItemMapper.selectById(se.getServeItemId());
            //如果不存在或为启用则抛出异常
            if (serveItem == null || serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
                throw new RuntimeException("服务项不存在或未启用");
            }
            //是否是重复提交
            Long count = lambdaQuery()
                    .eq(Serve::getRegionId, se.getRegionId())
                    .eq(Serve::getServeItemId, se.getServeItemId())
                    .count();
            if(count > 0){
                throw new RuntimeException("服务项已存在");
            }
            //添加
            Serve serve =BeanUtils.toBean(se, Serve.class);
            //价格
            serve.setPrice(serveItem.getReferencePrice());
            Region region = regionMapper.selectById(se.getRegionId());
            //城市编码
            serve.setCityCode(region.getCityCode());
            //保存
            save(serve);
        }
    }

    /**
     * 修改服务项价格
     *
     * @param id
     * @param price
     */
    @Override
    public void updatePrice(Long id, BigDecimal price) {

        boolean update = lambdaUpdate()
                .set(Serve::getPrice, price)
                .eq(Serve::getId, id)
                .update();
        if(!update){
            throw new RuntimeException("更改价格失败");
        }
    }

    @Override
    public void onSale(Long id) {
        //判断区域服务是否存在
        Serve serve = baseMapper.selectById(id);
        if(serve == null){
            throw new RuntimeException("服务不存在");
        }
        //判断服务是否是草稿或下架
        if(serve.getSaleStatus() != FoundationStatusEnum.DISABLE.getStatus() && serve.getSaleStatus() != FoundationStatusEnum.INIT.getStatus()){
            throw new RuntimeException("状态异常");
        }
        ServeItem serveItem = serveItemMapper.selectById(id);
        if(serveItem == null ){
            throw new RuntimeException("服务项不存在");
        }
        //判断服务项是否启动
        if(serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()){
            throw new RuntimeException("服务项未启动");
        }
        boolean update = lambdaUpdate()
                .set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .eq(Serve::getId, id)
                .update();
        if(!update){
            throw new RuntimeException("上架失败");
        }
    }
    @Override
    @Cacheable(value = RedisConstants.CacheName.SERVE,key="#id",cacheManager = RedisConstants.CacheManager.ONE_DAY)
    public Serve queryServeByIdCache(Long id) {
        return getById(id);
    }
}
